package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Paint;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import play.game;


import tag.constants;

@SuppressWarnings("serial")
public class menu extends JPanel implements constants{



menu_state state = new menu_state();
	public void gameOver(){
		
		state.game_over();
		setVisible(true);
		update();
	}
	
	
	public void pause(){
	if(isVisible()) hide_menu();
	else set_pause();
	}
	
	public void set_pause(){
		
		state.pause();
		show_this();
		update();
	}
	
	
	@SuppressWarnings("deprecation")
	public void show_this(){
		if(game.gameThread.isAlive())game.gameThread.suspend();
		setVisible(true);	
	}
	
	
	@SuppressWarnings("deprecation")
	public void hide_this(){
		game.gameThread.resume();
		setVisible(false);
	}
	
	
	public void hide_menu(){
		
		state.hide();
		hide_this();
		update();
	}
	
	public void main_menu(){
		if(isVisible()) hide_menu();
		else set_in_game_menu();
	}
	
	void set_in_game_menu(){
		state.in_game_menu();
		show_this();
		update();
	}

	void update(){
		JPanel comps = state.getComponents(this);
		if(comps!=null)add(comps);
	}
	
	public menu(){
		setOpaque(false);
		setPreferredSize(new Dimension(frame_width,frame_height));
		setLayout(new GridLayout(1,1));
		update();
	}
	
	
	public void paintComponent(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		
		state.print(g2);
	}
	
	
}






class menu_state
{

  private menu_child state;
  

  void print(Graphics2D g){
	  this.state.print(g);
  }
  public void in_game_menu() {
this.state = new InGameMenu();
	
}
public void pause() {

	this.state=new Pause();
  }
public JPanel getComponents(menu menu) {
	
	return state.getComponents(menu);
  }
public void hide() {
	
	this.state = new NullState();
  }
  
public menu_state()
  {
    this.state = new MainMenu();
  }
 
  
  public void game_over(){
	  this.state = new GameOver();
  }

 
}





interface menu_child extends constants{
  
	Font big = new Font("Georgia", Font.ITALIC, 100);
	Paint bg = new Color(0,0,0,150);
	void print(Graphics2D g);
	JPanel getComponents(menu menu);
  
}


class MainMenu implements menu_child{

	public void print(Graphics2D g) {
		g.setFont(big);
		
		FontMetrics metrics = g.getFontMetrics();
		
		g.setPaint(bg);
		g.fillRect(0, 0, frame_width,frame_height);
		g.setPaint(Color.white);
		
		g.drawString("Main Menu", (frame_width-metrics.stringWidth("Main Menu"))/2,200);
		
		
	}

	public JPanel getComponents(final menu menu) {
		menu.removeAll();
		JPanel temp = new JPanel();
		temp.setOpaque(false);
		temp.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.NORTH;
		c.gridy=0;
		c.gridx=0;
		c.weightx=1;
		c.weighty=0;
		
		button start = new button("New Game");
		start.addMouseListener(new MouseAdapter(){

			

			public void mouseReleased(MouseEvent e) {
				game.new_game();
				menu.setVisible(false);
				
			}
			
		});
		temp.add(start,c);
		
		
		button quit = new button("Exit Game");
		quit.addMouseListener(new MouseAdapter(){

			

			public void mouseReleased(MouseEvent e) {
			game.quit();
				
			}
			
		});
		c.gridy=1;
		temp.add(quit,c);
		
		
		
		return temp;
	}
}


class InGameMenu implements menu_child{

	public void print(Graphics2D g) {
		g.setFont(big);
		
		FontMetrics metrics = g.getFontMetrics();
		
		g.setPaint(bg);
		g.fillRect(0, 0, frame_width,frame_height);
		g.setPaint(Color.white);
		
		g.drawString("Main Menu", (frame_width-metrics.stringWidth("Main Menu"))/2,200);
		
		
	}

	public JPanel getComponents(final menu menu) {
		menu.removeAll();
		JPanel temp = new JPanel();
		temp.setOpaque(false);
		temp.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.NORTH;
		c.gridy=0;
		c.gridx=0;
		c.weightx=1;
		c.weighty=0;
		
		button resume = new button("Resume");
		resume.addMouseListener(new MouseAdapter(){

			

			public void mouseReleased(MouseEvent e) {
				
				menu.hide_menu();
				
			}
			
		});
		temp.add(resume,c);
		
		
		button quit = new button("Exit Game");
		quit.addMouseListener(new MouseAdapter(){

			

			public void mouseReleased(MouseEvent e) {
			game.quit();
				
			}
			
		});
		c.gridy=1;
		temp.add(quit,c);
		
		
		
		return temp;
	}
}

class NullState implements menu_child{
	public void print(Graphics2D g) {}
	public JPanel getComponents(menu menu) {menu.removeAll();return null;}
}

class GameOver implements menu_child{

	public void print(Graphics2D g) {
		
		g.setFont(big);
		
		FontMetrics metrics = g.getFontMetrics();
		
		g.setPaint(bg);
		g.fillRect(0, 0, frame_width,frame_height);
		g.setPaint(Color.white);
		
		g.drawString("Game Over", (frame_width-metrics.stringWidth("Game Over"))/2,(frame_height-metrics.getHeight())/2+20);
		
		
	}

	public JPanel getComponents(menu menu) {
		menu.removeAll();
		return null;
	}
}



class Pause implements menu_child{

	public void print(Graphics2D g) {
		g.setFont(big);
		
		FontMetrics metrics = g.getFontMetrics();
		
		g.setPaint(bg);
		g.fillRect(0, 0, frame_width,frame_height);
		g.setPaint(Color.white);
		
		g.drawString("game paused", (frame_width-metrics.stringWidth("game paused"))/2,(frame_height-metrics.getHeight())/2+20);
		
	}

	public JPanel getComponents(menu menu) {
		menu.removeAll();
		return null;
	}
}


@SuppressWarnings("serial")
class button extends JPanel implements constants{
	
	String value = "";
	Paint bg = new Color(0,0,0,100);
	Font font = new Font("Georgia", Font.ITALIC, 20);

	public button(String string){
		value = string;
		
		setOpaque(false);
		setPreferredSize(new Dimension(20,50));
	}
	public void paintChildren(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		g2.setFont(font);
		FontMetrics metrics = g.getFontMetrics();

		
		g2.setPaint(bg);
		g2.fillRect(0, 0,getWidth(),getHeight());
		g2.setPaint(Color.white);
		
		g2.drawString(value, (frame_width-metrics.stringWidth(value))/2,32);
	}
}

